package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Province;
import service.ProvinceService;
import service.impl.ProvinceServiceImpl;
import util.JsonUtil;

public class ProvinceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProvinceService ps;
	
    public ProvinceServlet() {
    	ps = new ProvinceServiceImpl();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		PrintWriter out = response.getWriter();
		
		String province = request.getParameter("province");
		if(province!=null){
			byte[] bs = province.getBytes("ISO-8859-1");
			province = new String(bs,"utf-8");
		}
		List<Province> list = ps.queryAllProvinceByName(province);
		out.write(JsonUtil.getJsonFromObject(list));
		out.flush();
	}
}
